﻿function InitCustomerContact() {

    if (GetCookie("gz_customer") != null) {


        $.getJSON("/ajax/ajaxorder.ashx", { methodName: "InitialCustomerOrderContact", r: Math.random() }, function (user) {
            $("#txt_CustomerName").val(user.CustomerName);
            $("#txt_TelNum").val(user.TelNum);
            $("#txt_Email").val(user.Email);
            setTimeout(function () {
                $("#sel_RegionId").each(function () {
                    for (var i = 0; i < this.options.length; i++) {
                        if (this.options[i].value == user.RegionId) {
                            this.options[i].selected = "selected";
                            break;
                        }
                    }
                });
            }, 5);
            setTimeout(function () {
                $("#sel_OfficeId").each(function () {
                    for (var i = 0; i < this.options.length; i++) {
                        if (this.options[i].value == user.OfficeId) {
                            this.options[i].selected = "selected";
                            break;
                        }
                    }
                });
            }, 5);
            setTimeout(function () {
                $("#sel_BusinessDistrictId").each(function () {
                    for (var i = 0; i < this.options.length; i++) {
                        if (this.options[i].value == user.BusinessDistrictId) {
                            this.options[i].selected = "selected";
                            break;
                        }
                    }
                });
            }, 5);
            setTimeout(function () {
                $("#sel_FloorId").each(function () {
                    for (var i = 0; i < this.options.length; i++) {
                        if (this.options[i].value == user.FloorId) {
                            this.options[i].selected = "selected";
                            break;
                        }
                    }
                });
            }, 5);
            $("#txt_Address").val(user.Address);
            // $("#txt_Memo").val(user.Memo);
        })
    }

}
//提交订单方法
function LoadEvent() { 
    $.formValidator.initConfig({
        theme: "126",
        formID: "form1",
        ajaxForm: {
            url: "/ajax/ajaxorder.ashx",
            type: "post",
            dataType: "text",
            cache: false,
            success: function (data) {

                if (data.indexOf("未登录") > -1) {
                    $.jBox.error('您好像还没有登录哦!', '');
                }
                else if (data.indexOf("您的购物车目前没有商品") > -1) {
                    $.jBox.error('购物车没有东西!', '');
                }
                else if (data.indexOf("订单发生异常") > -1) {
                    alert(data);
                }
                else if (data.indexOf("订单成功") > -1) {
                    //                    $.jBox.tip("正在提交订单中...", 'loading');
                    //                    window.setTimeout(function () {
                    //                        $.jBox.tip('提交订单成功。', 'success');
                    //                        window.location.href = "order_success.aspx";
                    //                    }, 1000);
                    $("#btnsumbit").attr("disabled", true);

                    //如果选择线上支付则跳转至支付页面
                    if ($("[name='pay_type']:checked").val() == "货到付款") {
                        window.location.href = "order_success.aspx";
                    }
                    else {
                        var OrderId = data.split(',')[1];
                        var OrderAmount = data.split(',')[2];
                        if ($("[name='pay_class']:checked").val() == "财付通") {
                            window.location.href = "/PaymentOnLine/PaymentInterface.aspx?Type=" + escape("财付通") + "&ProductName="+ escape("果汁") + "&OrderId=" + OrderId + "&OrderAmount=" + OrderAmount;
                        }
                        if ($("[name='pay_class']:checked").val() == "支付宝") {
                            window.location.href = "/PaymentOnLine/PaymentInterface.aspx?Type=" + escape("支付宝") + "&ProductName=" + escape("果汁") + "&OrderId=" + OrderId + "&OrderAmount=" + OrderAmount;
                        }
                    }
                }
                else if (data.indexOf("下一个") > -1) {
                    var msg = '您好,为了给工作人员留出制作与配送的时间,<br/>请您选择下一个配送时段:)我们会为您奉上一杯完美制作的果汁饮品!';
                    $("#btnsumbit").removeAttr("disabled");
                    $.jBox.error(msg);
                }
            },
            beforeSend: function () {
                $("#btnsumbit").attr("disabled", true);
                var time = $("#sel_DeliveryPeriodId").val();

                if ($("#sel_RegionId").val() == "-1") {
                    alert("请选择区!");
                    $("#btnsumbit").removeAttr("disabled");
                    return false;
                }
                if ($("#sel_BusinessDistrictId").val() == "-1") {
                    alert("请选择商圈!");
                    $("#btnsumbit").removeAttr("disabled");
                    return false;
                }
                if ($("#sel_OfficeId").val() == "-1") {
                    alert("请选择中心区!");
                    $("#btnsumbit").removeAttr("disabled");
                    return false;
                }
                if ($("#sel_FloorId").val() == "-1") {
                    alert("请选择楼层!");
                    $("#btnsumbit").removeAttr("disabled");
                    return false;
                }

                if ($("#sel_DeliveryPeriodId").val() == "-1") {
                    $.jBox.error('请选择配送时段!', '');
                    $("#btnsumbit").removeAttr("disabled");
                    return false;
                }
                if ($("#sel_FloorId").val() == "-1") {
                    $.jBox.error('请您不要忘记选择楼层哦!', '');
                    $("#btnsumbit").removeAttr("disabled");
                    return false;
                }

                if (cookie.Get("carList") == null) {
                    $.jBox.error('您的购物车目前没有商品!', '');
                    $("#btnsumbit").removeAttr("disabled");
                    return false;
                }
                return true;
            }
        },
        onError: function (msg) { alert(msg); },
        submitAfterAjaxPrompt: '有数据正在异步验证，请稍等...'
    });


    $("#txt_CustomerName").formValidator({ onShowFixText: "6~12个字符，包括字母、数字、下划线，以字母开头，字母或数字结尾", onShow: "请输入用户名", onCorrect: "" }).inputValidator({ min: 1, onError: "你输入的用户长度不正确,请确认" });
    $("#txt_TelNum").formValidator({ onShowFixText: "密码遗忘或被盗时，可通过手机短信取回密码", onShow: "请输入手机号码", onFocus: "手机的长度必须是11位", onCorrect: "" })
                    .inputValidator({
                        min: 8, max: 11, onError: "输入座机或者手机号码长度不正确!"
                    });
    //  $("#txt_Email").formValidator({ onShowFixText: "6~18个字符，包括字母、数字、下划线，以字母开头，字母或数字结尾", onShow: "请输入邮箱", onFocus: "邮箱6-100个字符,输入正确了才能离开焦点", onCorrect: "", defaultValue: "" }).inputValidator({ min: 6, max: 100, onError: "你输入的邮箱格式不正确,请确认" }).regexValidator({ regExp: "^([\\w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([\\w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$", onError: "你输入的邮箱格式不正确" })


    // $("#txt_Memo").formValidator({ onShowFixText: "", onShow: "请输入您的收货地址", onFocus: "描述至少要输入", onCorrect: "" });
    $("#txt_Address").formValidator({ onShowFixText: "", onShow: "请填写收货地址。", onFocus: "请填写收货地址。", onCorrect: "" });
    $("#txt_OrderMemo").formValidator({ onShowFixText: "如：常温、冰镇等口味偏好", onShow: "如：常温、冰镇等口味偏好", onFocus: "如：常温、冰镇等口味偏好", onCorrect: "" });
    $("#txt_DeliveryDate").formValidator({ onShowFixText: "", onShow: "", onFocus: "", onCorrect: "" })
                            .inputValidator({
                                min: 1,
                                onError: "请选择配送时间!"
                            });

    LoadDeliveryarea();
    BindPageSelEvent();
    LoadDeliveryDate();
}

function StopButton() {

    document.getElementById(arguments[0]).disabled = true;
    document.getElementById(arguments[0]).value = "提交中(" + arguments[1] + ")";
    if (--arguments[1] > 0) {
        window.setTimeout("StopButton('" + arguments[0] + "'," + arguments[1] + ")", 1000);

    }
    if (arguments[1] <= 0) {
        document.getElementById(arguments[0]).value = '提交';
        document.getElementById(arguments[0]).disabled = false;

    }
}

function toEmpty() {

    $("#txt_Email").val("");
    $("#txt_CustomerName").val("");
    $("#txt_TelNum").val("");
    // $("#txt_Memo").val("");
    $("#sel_RegionId").val("-1");
    $("#sel_BusinessDistrictId").val("-1");
    $("#sel_OfficeId").val("-1");
    $("#sel_FloorId").val("-1");
    $("#txt_PWD").val("");
    $("#txt_RepPWD").val("");
}
function LoadDeliveryarea(forOrderSubmit) {

    $.getJSON("/ajax/ajaxcustomer.ashx?methodName=LoadDeliveryarea", function (as) {
        $.each(as, function (i, item) {
            $("#sel_RegionId").append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");
        })
        // $("#sel_RegionId").prepend("<option value='-1'>-请选择-</option>");

        //            if (GetCookie("gz_customer") == null) {

        //                $("#sel_RegionId").val("-1");
        //            }
    })


    $.getJSON("/ajax/ajaxcustomer.ashx?methodName=LoadDeliveryarea&id=1", function (as) {
        $.each(as, function (i, item) {
            $("#sel_BusinessDistrictId").append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");
        })
        // $("#sel_BusinessDistrictId").prepend("<option value='-1'>-请选择-</option>");
    })
    $.getJSON("/ajax/ajaxcustomer.ashx?methodName=LoadDeliveryarea&id=2", function (as) {
        $.each(as, function (i, item) {
            $("#sel_OfficeId").append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");
        })
        $("#sel_OfficeId").prepend("<option value='-1'>-请选择-</option>");


        if (GetCookie("gz_customer") != null) {

            $("#sel_OfficeId").val($.evalJSON(GetCookie("gz_customer")).OfficeId);

        } else {
            $("#sel_OfficeId").val("-1");
        }



    })
    $.getJSON("/ajax/ajaxcustomer.ashx?methodName=LoadDeliveryarea&id=3", function (as) {
        $.each(as, function (i, item) {
            $("#sel_FloorId").append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");
        })
        $("#sel_FloorId").prepend("<option value='-1'>-请选择-</option>");
        if (GetCookie("gz_customer") != null) {
            setTimeout(function () {
                $("#sel_FloorId").each(function () {
                    for (var i = 0; i < this.options.length; i++) {
                        if (this.options[i].value == $.evalJSON(GetCookie("gz_customer")).FloorId) {
                            this.options[i].selected = "selected";
                            break;
                        }
                    }
                });
            }, 50);
        }
        else {
            setTimeout(function () {
                $("#sel_FloorId").each(function () {
                    for (var i = 0; i < this.options.length; i++) {
                        if (this.options[i].value == -1) {
                            this.options[i].selected = "selected";
                            break;
                        }
                    }
                });
            }, 50);
        }
    })
}

function LoadDeliveryDate() {

    $.getJSON("/ajax/ajaxcustomer.ashx?methodName=LoadDeliveryDate&r=" + Math.random(), function (deliveries) {
        if (deliveries) {
            $.each(deliveries, function (i, item) {
                $("#sel_DeliveryPeriodId").append("<option value='" + item.Id + "'>" + item.DeliveryPeriodName + "</option>");
            })
            $("#sel_DeliveryPeriodId").prepend("<option value='-1'>-请选择-</option>");


            setTimeout(function () {
                $("#sel_DeliveryPeriodId").each(function () {
                    for (var i = 0; i < this.options.length; i++) {
                        if (this.options[i].value == '-1') {
                            this.options[i].selected = "selected";
                            break;
                        }
                    }
                });
            }, 5);


        }
        else {
            $("#sel_DeliveryPeriodId").empty().append("<option value='-1'>-请选择-</option>");
        }
    });
}
function validateCustomLogin() {
    var name = $.trim($("#txtEmail").val());
    var pwd = $.trim($("#txtPwd").val());
    $.ajax({
        url: "/ajax/ajaxcustomer.ashx",
        type: "POST",
        data: { methodName: "CustomerLogin" },
        success: function (res) {
            if (res.indexOf("用户名或者密码输入不正确") > -1) {
                alert("您的用户名或者密码输入不正确!");
            }
            else if (res.indexOf("登录成功") > -1) {
                alert("登录成功!");
            }
        },
        beforeSend: function () {
            if (name == "") {
                alert("请填写您的用户名!");
                $("#txtEmail").focus();
                return false;
            }
            if (pwd == "") {
                alert("请填写您的密码!");
                $("#txtPwd").focus();
                return false;
            }
        },
        error: function (xhr) { alert(xhr.responseText); }
    })
}
function BindPageSelEvent() {
    $("#sel_RegionId").change(function () {
        if ($(this).val() != "-1") {
            $.getJSON("/ajax/ajaxcustomer.ashx?methodName=LoadDeliveryarea&id=" + $(this).val(), function (as) {
                if (!as) {
                    $("#sel_BusinessDistrictId").empty().append("<option value='-1'>-请选择-</option>");
                    $("#sel_OfficeId").empty().append("<option value='-1'>-请选择-</option>");
                    $("#sel_FloorId").empty().append("<option value='-1'>-请选择-</option>");
                }
                $.each(as, function (i, item) {
                    //  $("#sel_BusinessDistrictId").empty().append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");

                    $("#sel_BusinessDistrictId").append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");

                })
                //   $("#sel_BusinessDistrictId").prepend("<option value='-1'>-请选择-</option>");
                $("#sel_BusinessDistrictId").val("-1");
            })

        } else {

            $("#sel_BusinessDistrictId").empty().append("<option value='-1'>-请选择-</option>");
            $("#sel_OfficeId").empty().append("<option value='-1'>-请选择-</option>");
            $("#sel_FloorId").empty().append("<option value='-1'>-请选择-</option>");
        }
    })

    $("#sel_BusinessDistrictId").change(function () {
        if ($(this).val() != "-1") {
            $.getJSON("/ajax/ajaxcustomer.ashx?methodName=LoadDeliveryarea&id=" + $(this).val(), function (as) {
                if (!as) {
                    $("#sel_OfficeId").empty().append("<option value='-1'>-请选择-</option>");
                    $("#sel_FloorId").empty().append("<option value='-1'>-请选择-</option>");
                }
                $.each(as, function (i, item) {
                    //  $("#sel_OfficeId").empty().append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");
                    $("#sel_OfficeId").append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");
                })
                //  $("#sel_OfficeId").prepend("<option value='-1'>-请选择-</option>");
                $("#sel_OfficeId").val("-1");
            })

        } else {
            $("#sel_OfficeId").empty().append("<option value='-1'>-请选择-</option>");
            $("#sel_FloorId").empty().append("<option value='-1'>-请选择-</option>");
        }
    })



    $("#sel_OfficeId").change(function () {
        if ($(this).val() != "-1") {
            $.getJSON("/ajax/ajaxcustomer.ashx?methodName=LoadDeliveryarea&id=" + $(this).val(), function (as) {
                if (!as) {
                    $("#sel_FloorId").empty().append("<option value='-1'>-请选择-</option>");
                }
                $("#sel_FloorId").empty();
                $.each(as, function (i, item) {
                    //  $("#sel_FloorId").empty().append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");

                    $("#sel_FloorId").append("<option value=" + item.Id + ">" + item.DeliveryAreaName + "</option>");
                })
                $("#sel_FloorId").prepend("<option value='-1'>-请选择-</option>");
                $("#sel_FloorId").val("-1");

            })

        } else {
            $("#sel_FloorId").empty().append("<option value='-1'>-请选择-</option>");
        }
    })


    $("#sel_FloorId").change(function () {
        if ($(this).val() != "-1") {
            $.get("/ajax/ajaxcustomer.ashx?methodName=QueryFloorDefaultValue&id=" + $(this).val(), function (as) {

                if (as == "") {
                    $("#txt_Address").val("");
                } else {
                    $("#txt_Address").val(as);
                }
            })

        } else {
            $("#txt_Address").val("");
        }
    })




    //验证送货时段
    $("#sel_DeliveryPeriodId").change(function () {

        $.ajax({
            url: "/ajax/ajaxorder.ashx",
            data: { methodName: "ValidateDeliveryPeriodDate", periodId: $(this).val(), date: $("#txt_DeliveryDate").val() },
            cache: false,
            success: function (r) {
                if (r.indexOf("datenull") > -1) {
                    $.jBox.error('请选择配送时段!', '');
                }
                else if (r.indexOf("false") > -1) {
                    var msg = '您好,为了给工作人员留出制作与配送的时间,<br/>请您选择下一个配送时段:)我们会为您奉上一杯完美制作的果汁饮品!';
                    $.jBox.error(msg, '');
                }
                else if (r.indexOf("true") > -1) {
                    return;
                }
            }
        })


    })
}
 